using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.PageFlow;
using System.Web;
using System.Collections;
using Microsoft.Practices.PageFlow.Utils;
using WCSFContrib.PageFlow.Xml.Properties;
using WCSFContrib.PageFlow.Xml.Storage.Session;

namespace WCSFContrib.PageFlow.Xml.Storage.Session
{
    public class XmlPageFlowInstanceCorrelationSessionProvider : IPageFlowInstanceStore
    {
        private const string PageFlowInstanceCorrelationSessionProviderKey = "PageFlowInstanceCorrelationSessionProvider";

        public XmlPageFlowInstanceCorrelationSessionProvider()
        {
        }

        public XmlPageFlowInstanceCorrelationSessionProvider(string databaseName, IPageFlowCorrelationTokenProvider tokenProvider)
		{
		}

        protected virtual PageFlowInstaceSessionTracker State
        {
            get
            {
                HttpContext context = HttpContext.Current;
                if (context == null)
                    throw new InvalidOperationException(Resources.HttpContextNotAvailable);

                PageFlowInstaceSessionTracker state = context.Session[PageFlowInstanceCorrelationSessionProviderKey] as PageFlowInstaceSessionTracker;
                if (state == null)
                {
                    state = new PageFlowInstaceSessionTracker();
                    context.Session[PageFlowInstanceCorrelationSessionProviderKey] = state;
                }
                return state;
            }
        }

        public void Add(IPageFlow pageFlowInstance)
        {
            Guard.ArgumentNotNull(pageFlowInstance, "pageFlowInstance");
            this.State.Add(pageFlowInstance);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
        public void Remove(IPageFlow pageFlowInstance)
        {
            Guard.ArgumentNotNull(pageFlowInstance, "pageFlowInstance");
            Remove(pageFlowInstance.Id);
        }

        public void Remove(Guid id)
        {
            if (id == Guid.Empty)
                throw new ArgumentException(Resources.IdCannotBeEmpty);

            this.State.Remove(id);
        }

        public Guid GetLastRunningInstance()
        {
            return this.State.RunningInstance;
        }

        public Guid GetByType(Type type)
        {
            return this.State.GetByType(type);           
        }

        public string GetInstanceType(Guid id)
        {
            return this.State.GetInstanceType(id);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
        public void SetPageFlowRunning(IPageFlow iPageFlow)
        {
            Guard.ArgumentNotNull(iPageFlow, "iPageFlow");
            this.State.RunningInstance = iPageFlow.Id;
        }

        public void SetPageFlowNotRunning(IPageFlow iPageFlow)
        {
            this.State.RunningInstance = Guid.Empty;
        }
    }
}
